package com.lbm.baseclass.controller;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import com.lbm.baseclass.service.IBaseService;
import com.lbm.baseclass.utils.SpringBeanLoader;
import com.lbm.baseclass.utils.json.JsonUtil;
import com.lbm.model.RoleInfo;
import com.lbm.model.RoleUser;
import com.lbm.model.UserInfo;

public class LBMBaseController extends BaseController{

	protected static IBaseService baseService;

	static {
		if (baseService == null) {
			baseService = (IBaseService) SpringBeanLoader
					.getBean("baseService");
		}
	}
	protected String v_list=null;
	protected String v_index=null;
	protected String v_form=null;
	protected String v_success=null;
	public String getV_success() {
		return v_success;
	}
	public void setV_success(String v_success) {
		this.v_success = v_success;
	}
	public String getV_list() {
		return v_list;
	}
	public void setV_list(String v_list) {
		this.v_list = v_list;
	}
	public String getV_index() {
		return v_index;
	}
	public void setV_index(String v_index) {
		this.v_index = v_index;
	}
	public String getV_form() {
		return v_form;
	}
	public void setV_form(String v_form) {
		this.v_form = v_form;
	}
	@SuppressWarnings("unchecked")
	public void isAdmin(HttpServletRequest request,HttpServletResponse response){
		boolean flag=true;
		String role=request.getParameter("roleid");
		List<RoleInfo> roles=baseService.getValueObjectsByHQL("from RoleInfo where roleid='"+role+"'");
		String user=request.getParameter("userid");
		List<UserInfo> users=baseService.getValueObjectsByHQL("from UserInfo where userid='"+user+"'");
		if(roles.size()!=0){
			if("超级管理员".equals(roles.get(0).getRoleName()))flag=false;
		}
		if(users.size()!=0){
			if("admin".equals(users.get(0).getUserName()))flag=false;
		}
		renderJson(response,toJson("success",flag));
	}
	public boolean isAdmin(HttpServletRequest request){
		boolean flag=false;
		@SuppressWarnings("unchecked")
		List<UserInfo> users=baseService.getValueObjectsByHQL("from UserInfo where userid='"+getUserId(request)+"'");
		if(users.size()!=0){
			if(users.get(0).getUserName().startsWith("admin"))flag=true;
		}
		return flag;
	}
	public ModelAndView getModuleTree(HttpServletRequest request,HttpServletResponse response){
		String roleid=request.getParameter("roleid");
		String userid=request.getParameter("userid");
		String flag=request.getParameter("flag");
		if(roleid!=null){
			renderJson(response, baseService.getMenus(flag,roleid));
		}
		else if(userid!=null){
			@SuppressWarnings("unchecked")
			List<RoleUser> list=baseService.getValueObjectsByHQL("from RoleUser where userid='"+userid+"'");
			String[] roleids=new String[list.size()];
			for(int i=0;i<list.size();i++){
				roleids[i]=list.get(i).getRoleid();
			}
			renderJson(response, baseService.getMenus(flag,roleids));
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public Map<String, UserInfo> getAllUsers() {
		List<UserInfo> allusers = baseService.getValueObjectsByHQL("from UserInfo where 1=1");
		Map<String, UserInfo> users = new HashMap<String, UserInfo>();
		for (Object o : allusers) {
			UserInfo user = (UserInfo) o;
			users.put(user.getUserid(), user);
		}
		return users;
	}
	public void convertUserId(List<?> datas, String... fieldName) {
		convertList(datas, getAllUsers(), "UserNote", fieldName);
	}
	public <T> void convertList(List<?> datas, Map<String, T> source, String sourceField, String... fieldNames) {
		if (source == null || fieldNames == null || fieldNames.length == 0) return;
		for (String fieldName : fieldNames) {
			Method setter = null;
			Method getter = null;
			for (Object o : datas) {
				try {
					if (setter == null) {
						setter = o.getClass().getDeclaredMethod("set" + fieldName, String.class);
					}
					if (getter == null) {
						getter = o.getClass().getDeclaredMethod("get" + fieldName);
					}
					if (setter != null && getter != null) {
						T t = (T) source.get(getter.invoke(o));
						if (t != null) {
							Method gt = t.getClass().getDeclaredMethod("get" + sourceField);
							if (gt != null)	setter.invoke(o, gt.invoke(t));
						}
					}
				} catch (Exception e) {
				}
			}
		}
	}
	public void getUserToJson(HttpServletRequest request,
			HttpServletResponse response) {
		List<UserInfo> list=baseService.getValueObjectsByHQL("from UserInfo where 1=1");
		json.put("rows", list);
		renderJson(response, getJsonAndReset("roleUsers"));
	}
}
